#include "MainFrame/SettingDock/LayersDock/LayerDockWidget.h"
#include <QBoxLayout>
#include <QStandardItemModel>
#include <QToolButton>
#include <QHeaderView>
#include <QEvent>
#include <QInputDialog>

LayerDockWidget::LayerDockWidget(QWidget *parent, Qt::WindowFlags f) :
    QWidget(parent, f),
    m_pLayersTableView(nullptr),
    m_LayersModel(nullptr),
    m_pAddLayerButton(nullptr),
    m_pDelLayerButton(nullptr),
    m_pUpLayerButton(nullptr),
    m_pDownLayerButton(nullptr)
{
    initWidget();
    connetSignalToSlots();
}

LayerDockWidget::~LayerDockWidget()
{

}

void LayerDockWidget::onDataChanged(QModelIndex tl, QModelIndex br, QVector<int> vec)
{
    //    --------------checkstate changed----------------------


    int statechangedrow = tl.row();
    Qt::CheckState state = m_LayersModel->item(statechangedrow, 0)->checkState();
}

void LayerDockWidget::onClicked(QModelIndex selected)
{
    //    --------------Set Current Layer-------------------
    int selectedrow = selected.row();
}

void LayerDockWidget::initBottomLayer(const QString &strImageName)
{
    QStandardItem* layeritem = new QStandardItem();
    layeritem->setCheckable(true);
    layeritem->setCheckState(Qt::Checked);
    layeritem->setIcon(QIcon(":/Icons/Layers/BottomLayerIcon.png"));
    layeritem->setText(strImageName);

    m_LayersModel->insertRow(0, layeritem);
    m_LayersModel->setHeaderData(0, Qt::Vertical, "  ");
    m_pLayersTableView->selectRow(0);
    //    --------------Set Current Layer-------------------
}

void LayerDockWidget::onAddLayer()
{
    QString strLayerName = QInputDialog(this).getText(this, "New Layer", "Input Layer Name: ");

    QStandardItem* layeritem = new QStandardItem();
    layeritem->setCheckable(true);
    layeritem->setCheckState(Qt::Checked);
    layeritem->setIcon(QIcon(":/Icons/Layers/UpperLayerIcon.png"));
    layeritem->setText(strLayerName);

    int selectedrow = m_pLayersTableView->currentIndex().row();
    if(selectedrow == -1)
        selectedrow =  0;
    m_LayersModel->insertRow(selectedrow, layeritem);
    m_LayersModel->setHeaderData(selectedrow, Qt::Vertical, "  ");
    m_pLayersTableView->selectRow(selectedrow);
    //    --------------Set Current Layer-------------------
}

void LayerDockWidget::onDelLayer()
{
    int currow = m_pLayersTableView->currentIndex().row();
    m_LayersModel->removeRow(currow);
}

void LayerDockWidget::onUpLayer()
{
    int currow = m_pLayersTableView->currentIndex().row();
    if(currow == 0)
        return;
    QList<QStandardItem *> listItem = m_LayersModel->takeRow(currow);
    m_LayersModel->insertRow(currow - 1,listItem);
    m_LayersModel->setHeaderData(currow - 1, Qt::Vertical, "  ");
    m_pLayersTableView->selectRow(currow - 1);
}

void LayerDockWidget::onDownLayer()
{
    int currow = m_pLayersTableView->currentIndex().row();
    int rowmax = m_LayersModel->rowCount();
    if(currow == rowmax - 1)
        return;
    QList<QStandardItem *> listItem = m_LayersModel->takeRow(currow);
    m_LayersModel->insertRow(currow + 1,listItem);
    m_LayersModel->setHeaderData(currow + 1, Qt::Vertical, "  ");
    m_pLayersTableView->selectRow(currow + 1);
}

void LayerDockWidget::initWidget()
{
    QVBoxLayout* mainlay = new QVBoxLayout;
    this->setLayout(mainlay);

    initTableViewAndModel();
    mainlay->addWidget(m_pLayersTableView);

    QHBoxLayout* toollay = new QHBoxLayout;
    mainlay->addLayout(toollay);

    initToolsButton();
    toollay->addWidget(m_pAddLayerButton);
    toollay->addWidget(m_pDelLayerButton);
    toollay->addWidget(m_pUpLayerButton);
    toollay->addWidget(m_pDownLayerButton);
}

void LayerDockWidget::initTableViewAndModel()
{
    m_pLayersTableView = new QTableView(this);
    m_pLayersTableView->horizontalHeader()->setStretchLastSection(true);
    m_pLayersTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_pLayersTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pLayersTableView->setSelectionMode(QAbstractItemView::SingleSelection);

    m_LayersModel = new QStandardItemModel(this);
    m_pLayersTableView->setModel(m_LayersModel);

    m_LayersModel->setColumnCount(1);
    m_LayersModel->setHeaderData(0, Qt::Horizontal, "Layers");
}

void LayerDockWidget::initToolsButton()
{
    m_pAddLayerButton = new QToolButton(this);
    m_pAddLayerButton->setIconSize(QSize(35, 35));
    m_pAddLayerButton->setIcon(QIcon(":/Icons/Layers/AddLayer.png"));

    m_pDelLayerButton = new QToolButton(this);
    m_pDelLayerButton->setIconSize(QSize(35, 35));
    m_pDelLayerButton->setIcon(QIcon(":/Icons/Layers/DeleteLayer.png"));

    m_pUpLayerButton = new QToolButton(this);
    m_pUpLayerButton->setIconSize(QSize(35, 35));
    m_pUpLayerButton->setIcon(QIcon(":/Icons/Layers/LayerUp.png"));

    m_pDownLayerButton = new QToolButton(this);
    m_pDownLayerButton->setIconSize(QSize(35, 35));
    m_pDownLayerButton->setIcon(QIcon(":/Icons/Layers/LayerDown.png"));
}

void LayerDockWidget::connetSignalToSlots()
{
    connect(m_pAddLayerButton, SIGNAL(clicked(bool)), this, SLOT(onAddLayer()));
    connect(m_pDelLayerButton, SIGNAL(clicked(bool)), this, SLOT(onDelLayer()));
    connect(m_pUpLayerButton, SIGNAL(clicked(bool)), this, SLOT(onUpLayer()));
    connect(m_pDownLayerButton, SIGNAL(clicked(bool)), this, SLOT(onDownLayer()));

    connect(m_LayersModel, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
            this, SLOT(onDataChanged(QModelIndex,QModelIndex,QVector<int>)));
    connect(m_pLayersTableView, SIGNAL(clicked(QModelIndex)),
            this, SLOT(onClicked(QModelIndex)));
}
